//
//  LGConsultRecordHistoryCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/18.
//  吸烟史 、 饮酒史

#import "LGConsultRecordHistoryCell.h"
#import "LGPickerViewMax.h"
#define btnW   (Screen_W-viewPix(64))/2.0
#define btnH    viewPix(32)
@interface LGConsultRecordHistoryCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *subTitleLabel;
/**   */
@property (nonatomic , strong)UIButton *haveBtn;
/**   */
@property (nonatomic , strong)UIButton *haventBtn;
/**   */
@property (nonatomic , strong)UIView *bottomView;
/**   */
@property (nonatomic , strong)UILabel *yearTitle;
/**   */
@property (nonatomic , strong)UITextField *yearTF;
/**   */
@property (nonatomic , strong)UILabel *yearUnit;
/**   */
@property (nonatomic , strong)UILabel *stopTitle;
/**   */
@property (nonatomic , strong)UIButton *stopButton;
/**   */
@property (nonatomic , strong)NSArray *stopArray;

@end

@implementation LGConsultRecordHistoryCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setType:(NSString *)type{
    _type = type;
    if([type isEqualToString:@"smoke"]){
        //吸烟史
        self.titleLabel.text = @"吸烟史";
        self.subTitleLabel.text = @"请选择是否有吸烟习惯?";
        self.yearTitle.text = @"请输入吸烟年限";
        self.stopTitle.text = @"请选择是否已戒烟？";
        self.stopArray = @[@"已戒烟",@"未戒烟"];
    }else{
        //饮酒史
        self.titleLabel.text = @"饮酒史";
        self.subTitleLabel.text = @"请选择是否有饮酒习惯?";
        self.yearTitle.text = @"请输入饮酒年限";
        self.stopTitle.text = @"请选择是否已戒酒？";
        self.stopArray = @[@"已戒酒",@"未戒酒"];
    }
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *have = [self.type isEqualToString:@"smoke"]?LGNSString(dataDic[@"sfxy"]):LGNSString(dataDic[@"sfyj"]);
    NSString *stop = [self.type isEqualToString:@"smoke"]?LGNSString(dataDic[@"sfjy"]):LGNSString(dataDic[@"sfjj"]);
    self.haveBtn.selected = [have isEqualToString:@"有"]?YES:NO;
    self.haventBtn.selected = [have isEqualToString:@"无"]?YES:NO;
    self.yearTF.text = LGNSString(dataDic[@"year"]);
    self.stopButton.selected = (stop.length>0)?YES:NO;
    [self.stopButton setTitle:stop forState:UIControlStateSelected];
    
    CGFloat bottomHeight = [have isEqualToString:@"有"] ? viewPix(137):0;
    [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(bottomHeight));
    }];
}

//有 、 无
-(void)haveOrNotButtonTouched:(UIButton *)sender{
    self.haveBtn.selected = (sender == self.haveBtn)?YES:NO;
    self.haventBtn.selected = (sender == self.haveBtn)?NO:YES;
    if(self.delegate && [self.delegate respondsToSelector:@selector(haveSmokeOrDrinkHistory:have:)]){
        [self.delegate haveSmokeOrDrinkHistory:self.type have:sender.titleLabel.text];
    }
}

//是否戒断
-(void)stopButtonTouched{
    [self.yearTF resignFirstResponder];
    LGPickerViewMax *pickerView = [[LGPickerViewMax alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.pickArry = @[self.stopArray];
    pickerView.selectStr = [self.stopButton titleForState:UIControlStateSelected];
    [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectPickViewResult = ^(NSArray *resultArray) {
        weakSelf.stopButton.selected = YES;
        [weakSelf.stopButton setTitle:resultArray[0] forState:UIControlStateSelected];
        if(weakSelf.delegate && [self.delegate respondsToSelector:@selector(haveStopedSmokeOrDrink:stoped:)]){
            [self.delegate haveStopedSmokeOrDrink:weakSelf.type stoped:resultArray[0]];
        }
    };
}


//输入年份
-(void)textFieldEditingChanged:(UITextField *)textField{
    NSString *content = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    content = (content.length>2)?[content substringToIndex:2]:content;
    self.yearTF.text = content;
    if(self.delegate && [self.delegate respondsToSelector:@selector(smokeOrDrinkYearChanged:year:)]){
        [self.delegate smokeOrDrinkYearChanged:self.type year:content];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.subTitleLabel];
    [self.baseView addSubview:self.haveBtn];
    [self.baseView addSubview:self.haventBtn];
    [self.baseView addSubview:self.bottomView];
    [self.bottomView addSubview:self.yearTitle];
    [self.bottomView addSubview:self.yearTF];
    [self.bottomView addSubview:self.yearUnit];
    [self.bottomView addSubview:self.stopTitle];
    [self.bottomView addSubview:self.stopButton];
   
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(98));
        make.left.right.bottom.equalTo(self.baseView);
        make.height.equalTo(@(0));
    }];
    [self.yearUnit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.yearTF.mas_right).offset(viewPix(8));
        make.centerY.equalTo(self.yearTF);
    }];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B2BDC7" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(12), viewPix(6), viewPix(100), viewPix(20));
    }
    return _titleLabel;
}

-(UILabel *)subTitleLabel{
    if(!_subTitleLabel){
        _subTitleLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
        _subTitleLabel.frame = CGRectMake(viewPix(12), kMaxY(self.titleLabel.frame)+viewPix(8), viewPix(200), viewPix(20));
    }
    return _subTitleLabel;
}

-(UIButton *)haveBtn{
    if(!_haveBtn){
        _haveBtn = [UIButton buttonWithTitle:@"有" titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        _haveBtn.frame = CGRectMake(viewPix(12), viewPix(62), btnW, btnH);
        _haveBtn.cornerRidus = btnH/2.0;
        [_haveBtn setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [_haveBtn setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        [_haveBtn addTarget:self action:@selector(haveOrNotButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _haveBtn;
}

-(UIButton *)haventBtn{
    if(!_haventBtn){
        _haventBtn = [UIButton buttonWithTitle:@"无" titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        _haventBtn.frame = CGRectMake(kMaxX(self.haveBtn.frame)+viewPix(8), viewPix(62), btnW, btnH);
        _haventBtn.cornerRidus = btnH/2.0;
        [_haventBtn setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [_haventBtn setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        [_haventBtn addTarget:self action:@selector(haveOrNotButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _haventBtn;
}

-(UIView *)bottomView{
    if(!_bottomView){
        _bottomView = [[UIView alloc] init];
        _bottomView.clipsToBounds = YES;
    }
    return _bottomView;
}

-(UILabel *)yearTitle{
    if(!_yearTitle){
        _yearTitle = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
        _yearTitle.frame = CGRectMake(viewPix(12), viewPix(6), viewPix(150), viewPix(20));
    }
    return _yearTitle;
}

-(UITextField *)yearTF{
    if(!_yearTF){
        _yearTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(31), Screen_W-viewPix(84), viewPix(32))];
        _yearTF.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
        _yearTF.layer.borderWidth = 1;
        _yearTF.cornerRidus = 5;
        _yearTF.tintColor = [UIColor colorWithString:@"#333333"];
        _yearTF.textColor = [UIColor colorWithString:@"#333333"];
        _yearTF.textAlignment = NSTextAlignmentLeft;
        _yearTF.keyboardType = UIKeyboardTypeNumberPad;
        _yearTF.placeholder = @"请输入";
        _yearTF.font = LGFont(14);
        _yearTF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(12), viewPix(32))];
        _yearTF.leftViewMode = UITextFieldViewModeAlways;
        [_yearTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _yearTF;
}

-(UILabel *)yearUnit{
    if(!_yearUnit){
        _yearUnit = [UILabel lableWithText:@"年" colorString:@"#333333" textFont:LGFont(14) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _yearUnit;
}

-(UILabel *)stopTitle{
    if(!_stopTitle){
        _stopTitle = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
        _stopTitle.frame = CGRectMake(viewPix(12), kMaxY(self.yearTF.frame)+viewPix(8), viewPix(200), viewPix(20));
    }
    return _stopTitle;
}

-(UIButton *)stopButton{
    if(!_stopButton){
        _stopButton = [UIButton buttonWithTitle:@"请选择" titleFont:LGFont(14) normalColor:@"#999999" selectColor:@"#333333"];
        _stopButton.frame = CGRectMake(viewPix(12), kMaxY(self.stopTitle.frame)+viewPix(8), Screen_W-viewPix(59), viewPix(32));
        _stopButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _stopButton.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(12), 0, 0);
        _stopButton.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
        _stopButton.layer.borderWidth = 1;
        _stopButton.cornerRidus = 5;
        [_stopButton addTarget:self action:@selector(stopButtonTouched) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *tipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"grayTip12"]];
        tipView.frame = CGRectMake(Screen_W-viewPix(59)-viewPix(25), viewPix(10), viewPix(12), viewPix(12));
        [_stopButton addSubview:tipView];
    }
    return _stopButton;
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
